#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          svc-sampled
# Required-start:    
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: svc sample service
# Description:       start/stop svc sample service
### END INIT INFO
# chkconfig: 2345 20 80
# description: svc sample service
# processname: svc

# Set environment variables
export HOME=/root
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

CMDNAME=svc
CMDLINE=/opt/svc-sample/svc

# Define the start function
function start()
{
    echo "Starting ${CMDNAME}..."
    export LD_LIBRARY_PATH=`dirname "${CMDLINE}"`
    nohup "${CMDLINE}" 1>/dev/null 2>/dev/null &
}

# Define the stop function
function stop()
{
    local pids=`pidof "${CMDLINE}"`

    echo "Stopping ${CMDNAME}[${pids}]..."
    for pid in ${pids};
    do
        kill -9 ${pid}
    done
}

# Define the restart function
function restart()
{
  stop
  start
}

# Define the status function
function status()
{
    local pids=`pidof "${CMDLINE}"`

    if [ ! -z "${pids}" ]; then
        echo "svc-sample service is started"
    else
        echo "svc-sample service is stopped"
    fi
}

# Handle the command line arguments
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  status)
    status
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit 0
